package com.example.zhinongbao.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.OrderAdapter;
import com.example.zhinongbao.contract.OrderContract;
import com.example.zhinongbao.model.Order;
import com.example.zhinongbao.presenter.OrderPresenter;
import java.util.List;

public class OrderManagementFragment extends Fragment implements OrderContract.View {
    private static final String ARG_ORDER_STATUS = "order_status";
    private String currentStatus;
    private RecyclerView recyclerView;
    private OrderAdapter adapter;
    private TextView tvEmpty;
    private OrderContract.Presenter presenter;

    public static OrderManagementFragment newInstance(String orderStatus) {
        OrderManagementFragment fragment = new OrderManagementFragment();
        Bundle args = new Bundle();
        args.putString(ARG_ORDER_STATUS, orderStatus);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            currentStatus = getArguments().getString(ARG_ORDER_STATUS);
        }
        presenter = new OrderPresenter(this, requireContext());
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_management, container, false);
        recyclerView = view.findViewById(R.id.recycler_orders);
        tvEmpty = view.findViewById(R.id.tv_empty);

        // 设置空视图的文本为"暂时还没有订单"
        tvEmpty.setText("暂时还没有订单");

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new OrderAdapter();
        recyclerView.setAdapter(adapter);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.loadOrders(currentStatus);
    }

    @Override
    public void showOrders(List<Order> orders) {
        requireActivity().runOnUiThread(() -> {
            if (orders != null && !orders.isEmpty()) {
                recyclerView.setVisibility(View.VISIBLE);
                tvEmpty.setVisibility(View.GONE);
                adapter.updateOrders(orders);
            } else {
                // 没有订单数据时显示空视图
                recyclerView.setVisibility(View.GONE);
                tvEmpty.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void showEmpty() {
        requireActivity().runOnUiThread(() -> {
            recyclerView.setVisibility(View.GONE);
            tvEmpty.setVisibility(View.VISIBLE);
        });
    }

    @Override
    public void showError(String message) {
        requireActivity().runOnUiThread(() -> {
            // 将错误消息改为显示"暂时还没有订单"
            recyclerView.setVisibility(View.GONE);
            tvEmpty.setText("暂时还没有订单");
            tvEmpty.setVisibility(View.VISIBLE);
        });
    }
}